<template>
  <div class="home-container">
    <el-container style="height: 100vh;">
      <!-- 左侧菜单栏 -->
      <el-aside class="sidebar">
        <div class="logo">美容沙龙<br/>预约系统</div>
        <el-menu
            class="menu"
            :default-active="activeMenu"
            @select="highlightMenuOnly"
            background-color="#F9F7F1"
            text-color="#7C6651"
            active-text-color="#5A4A39"
        >
          <el-menu-item index="merchant">商家账户管理</el-menu-item>
          <el-menu-item index="customer">顾客信息管理</el-menu-item>
          <el-menu-item index="today">查看当日预约信息</el-menu-item>
          <el-menu-item index="history">查看历史预约信息</el-menu-item>
          <el-menu-item index="time">可预约时间管理</el-menu-item>
          <el-menu-item index="logout">登出</el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 右侧内容区域 -->
      <el-main class="main-panel">
        <div class="header">
          <span>你好，{{ admin.username || '先生' }}</span>
          <el-button class="logout-button" type="text" @click="confirmLogout">登出</el-button>
        </div>

        <div class="card-grid">
          <div
              v-for="item in cards"
              :key="item.index"
              class="card"
              :class="{ active: activeMenu === item.index }"
              @click="handleMenuClick(item.index)"
          >
            <i :class="item.icon"></i>
            <div class="label">{{ item.label }}</div>
          </div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      activeMenu: 'merchant',
      admin: {},
      cards: [
        { index: 'merchant', label: '商家账户管理', icon: 'el-icon-user-solid' },
        { index: 'customer', label: '顾客信息管理', icon: 'el-icon-user' },
        { index: 'today', label: '查看当日预约信息', icon: 'el-icon-date' },
        { index: 'history', label: '查看历史预约信息', icon: 'el-icon-time' },
        { index: 'time', label: '可预约时间管理', icon: 'el-icon-alarm-clock' },
        { index: 'logout', label: '登出', icon: 'el-icon-right' },
      ]
    };
  },
  created() {
    this.fetchAdminInfo();
  },
  methods: {
    goto(url) {
      this.$router.push(url);
    },
    highlightMenuOnly(index) {
      this.activeMenu = index;
      if (index === 'logout') {
        this.confirmLogout();
      }
    },
    handleMenuClick(index) {
      this.activeMenu = index;
      if (index === 'logout') {
        this.confirmLogout();
      } else {
        this.goto(`/${index}`);
      }
    },
    fetchAdminInfo() {
      axios.get('http://localhost:8080/user/admin/detail', { withCredentials: true })
          .then(response => {
            this.admin = response.data.data;
          })
          .catch(error => {
            console.error('获取管理员信息失败', error);
          });
    },
    confirmLogout() {
      this.$confirm('您确定要登出吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get('http://localhost:8080/user/logout', { withCredentials: true })
            .then(() => {
              this.$message.success('登出成功');
              window.location.href = '/login';
            })
            .catch(error => {
              console.error('登出失败', error);
              this.$message.error('登出失败');
            });
      }).catch(() => {
        // 用户点击取消
      });
    }
  }
};
</script>

<style scoped>
.home-container {
  height: 100vh;
  background-color: #F9F7F1;
  font-family: 'Microsoft YaHei', sans-serif;
}

.el-container {
  height: 100%;
}

.sidebar {
  background-color: #F9F7F1;
  padding-top: 20px;
  width: 16.6666%; /* 1/6 屏幕 */
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.05);
}

.logo {
  font-size: 18px;
  font-weight: bold;
  color: #7C6651;
  text-align: center;
  margin-bottom: 20px;
}

.menu {
  border-right: none;
}

.main-panel {
  width: 83.3333%; /* 5/6 屏幕 */
  padding: 40px;
  background-color: #F9F7F1;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
}

.header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
  font-size: 16px;
  color: #5A4A39;
}

.logout-button {
  color: #5A4A39;
  font-weight: bold;
}

.card-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 20px;
}

.card {
  height: 120px;
  background-color: #F9F7F1;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #7C6651;
  box-shadow: inset 0 0 0 transparent;
  cursor: pointer;
  transition: all 0.3s;
}

.card.active {
  background-color: #EADFC9;
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.1);
  color: #5A4A39;
  font-weight: bold;
}

.card i {
  font-size: 32px;
  margin-bottom: 10px;
}

.card .label {
  font-size: 14px;
}
</style>
